const cartModel = require("../model/cart")
class Cart{
    /**
     * 获取购物车
     * @param {*} ctx 
     */
    static async getCart(ctx){
        let {uid} = ctx.query
        let list = await cartModel.get(uid)
        ctx.body = {
            code:200,
            codeInfo:"成功",
            data:list
        }
    }
    /**
     * 添加购物车
     * @param {*} ctx 
     */
    static async addCart(ctx){
        let {goodsid,num,uid,skuid,skuname,temp_key} = ctx.request.body
        if(!temp_key && !uid || !goodsid || !num || !skuid || !skuname){
            ctx.body={
                code:201,
                codeInfo:"参数不足"
            }
        }else{
            let res = await cartModel.add(goodsid,num,uid,skuid,skuname,temp_key)
            if(res.type==='error'){
                ctx.body={
                    code:202,
                    codeInfo:res.msg
                }   
            }else{
                ctx.body={
                    code:200,
                    codeInfo:"添加成功",
                    data:res.data
                }
            }
        }
    }
    /**
     * 修改购物车
     * @param {*} ctx 
     */
    static async editCart(ctx){
        let {goodsid,skuid,id,num} = ctx.request.body
        if(!goodsid || !skuid || !id || !num){
             ctx.body={
                code:201,
                codeInfo:"参数不足"
            }
        }else{
            let res = await cartModel.eidt(goodsid,skuid,id,num)
            if(res.type==='error'){
                ctx.body={
                    code:202,
                    codeInfo:res.msg
                }   
            }else{
                ctx.body={
                    code:200,
                    codeInfo:"添加成功",
                    data:res.data
                }
            }
        }
    }

    /**
     * 删除购物车
     * @param {*} ctx 
     */
    static async delCart(ctx){
        let {idArr} = ctx.query
        let res = await cartModel.del(idArr)
        ctx.body = {
            code:200,
            codeInfo:"成功",
            data:res
        }
    }
}
module.exports = Cart